﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using androidpost.Object;
using androidpost.Utils;
using System.Threading;
using Newtonsoft.Json.Linq;
using System.Windows.Forms;


namespace androidpost.Network
{
    class NetworkSupport
    {

        public static OJStatus checkLogin(string username,string password){

            var data = new Dictionary<string, string>();
            data.Add("username", username);
            data.Add("password", password);
            data.Add("device", "other");
            
            JavaScriptSerializer ser = new JavaScriptSerializer();
            var JSONString = ser.Serialize(data); //JSON encoded

            string ok = methodPost(Constant.CONTENTTYPE_JSON, Constant.HEADER_GCM, Utils.Constant.URL_LOGIN, JSONString);
            Utils1.log(ok);
            
            try
            {
                 var JSONObj = ser.Deserialize<OJStatus>(ok); 
            return JSONObj;
            }
            catch (Exception)
            {
                Utils1.log(" (6834)have error");
               
            }

            var ret= new OJStatus();
            ret.code=101;
            ret.result="Network fail!";
            return ret;
           
           
        }


        //--------------------------Method sendControl--------------------------
        public static void sendControl(int code_control,int type,string value)
        {
            string valu1 = value.Replace("'","''");
            
            var data = new Dictionary<string, string>();
            data.Add("code_control", code_control+"");
            data.Add("id", Constant.id);
            data.Add("type", type+"");
            data.Add("value", valu1);
            data.Add("device", "other");

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var JSONString = ser.Serialize(data); //JSON encoded
           
            string ok = methodPost(Constant.CONTENTTYPE_JSON, Constant.HEADER_GCM, Utils.Constant.URL_SEND_PLAYLIST, JSONString);

          
            Utils1.log(ok);
            var JSONObj = Utils1.stringToOJStatus(ok);

            if (JSONObj.code != 0)
            {
                System.Windows.Forms.MessageBox.Show("Have error! \n data can't send to phone");
            }
            else notifyDevice(Constant.TYPE_PLAYLIST);
        }

        //--------------------------Method getControl--------------------------
        public static OJControl getControl()
        {
            var JSONObj=new OJStatus();
            var data = new Dictionary<string, string>();
            data.Add("id", Constant.id);
            data.Add("device", "other");

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var JSONString = ser.Serialize(data); //JSON encoded
           
            string ok = methodPost(Constant.CONTENTTYPE_JSON, Constant.HEADER_GCM, Utils.Constant.URL_GET_CONTROL, JSONString);
           
            //System.Windows.Forms.MessageBox.Show(ok);
            OJControl Control = Utils1.stringToOJControl(Utils1.stringToOJStatus(ok).data);

            return Control;

           
        }


        //--------------------------Method notifyDevice--------------------------
        public static OJStatus notifyDevice(int type) 
        {
            string send = type + "--" + Constant.id;
            string message = "\"" + send + "\"";
            string postData1 = "{ \"registration_ids\": [ \""+Constant.GCM_REGID+"\" ], \"data\": { \"price\": ";
            string postData2 = " } }";
            string dataSend = postData1 + message + postData2;

            string result = methodPost(Constant.CONTENTTYPE_JSON,Constant.HEADER_GCM, Utils.Constant.URL_GCM, dataSend);
            
            return null;
        }


        //--------------------------Method Post--------------------------
        public static string methodPost(string contenttype,string header,string url,string body)
        {
            Utils1.log("*******************POST****************************");
            Utils1.log("url send: " + url);
            Utils1.log("body send: "+body);

           // string fixBody = body.Replace();
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            if(header!=null) request.Headers.Add(header);
            request.ContentType = contenttype; 
            byte[] byteArray = Encoding.UTF8.GetBytes(body);
            request.ContentLength = byteArray.Length;

            Stream dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            var response = request.GetResponse();
            
            Stream stream2 = response.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);
            String ok = reader2.ReadToEnd();
            Utils1.log("json result: " + ok);
            Utils1.log("*******************END****POST**********************");
            return ok;
        }

    }
}
